﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class UserInfoForm : Form
    {
        private UserIoginForm userIoginForm = null;

        private UserInfoBLL _userInfoBLL = new UserInfoBLL();
        private UserInfo _userInfo = new UserInfo();
        public UserInfoForm(UserIoginForm userIoginForm):this()
        {
            this.userIoginForm = userIoginForm;
        }
        public UserInfoForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void UserInfoForm_Load(object sender, EventArgs e)
        {
            InitialData();

        }

        //调用BBL获取数据库中数据
        public void InitialData()
        {
            dataGridView1.DataSource = _userInfoBLL.UserInfoSelect();

        }

        private void UserInfoForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            userIoginForm.Close();
        }

        //添加
        private void button1_Click(object sender, EventArgs e)
        {
            //获取用户输入的参数
            string username = textBox2.Text;
            string password = textBox3.Text;
            string realname = textBox4.Text;
            string position = radioButton1.Checked ? "员工" : "经理";

            _userInfo.Username = username;
            _userInfo.Password = password;
            _userInfo.Realname = realname;
            _userInfo.Position = position;
            //无参数，调用BLL

            if (_userInfoBLL.ADD(_userInfo) > 0)
            { 
                MessageBox.Show("添加成功"); 
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                radioButton1.Checked = true;
            }
            else MessageBox.Show("添加失败");

            InitialData();
        }


        //修改数据
        private void button2_Click(object sender, EventArgs e)
        {

            //获取用户输入的参数
            int uid = Convert.ToInt32(textBox1.Text);
            string username = textBox2.Text;
            string password = textBox3.Text;
            string realname = textBox4.Text;
            string position = radioButton1.Checked ? "员工" : "经理";

            _userInfo.Uid = uid;
            _userInfo.Username = username;
            _userInfo.Password = password;
            _userInfo.Realname = realname;
            _userInfo.Position = position;
            //无参数，调用BLL

            if (_userInfoBLL.UPDATE(_userInfo) > 0)
            {
                MessageBox.Show("修改成功");
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                radioButton1.Checked = true;
            }
            else MessageBox.Show("修改失败");

            InitialData();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //双击获取选中的第1行第1个单元格
            textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();

            radioButton1.Checked = dataGridView1.SelectedRows[0].Cells[4].Value.ToString() == "员工" ? true : false;
            radioButton2.Checked = dataGridView1.SelectedRows[0].Cells[4].Value.ToString() == "经理" ? true : false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //获取用户输入的参数

            int uid = Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value);
            
            
            //无参数，调用BLL

            if (_userInfoBLL.DELETE(uid) > 0)
            {
                MessageBox.Show("删除成功");
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                radioButton1.Checked = true;
            }
            else MessageBox.Show("删除成功");

            InitialData();
        }
    }
}
